<#assign list="List"/>
<#assign integer="Integer"/>
<#assign Boolean="Boolean"/>
package ${geneGlobalConfig.basePackage}.controller;

import ${geneGlobalConfig.basePackage}.${geneGlobalConfig.entry}.${geneTable.tableName};
import ${geneGlobalConfig.basePackage}.service.${geneTable.tableName}Service;
import com.cbz.template.common.PageInfo;
import com.cbz.template.common.PageResult;
import ${geneGlobalConfig.basePackage}.common.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
<#if geneGlobalConfig?? && "" != geneGlobalConfig.author>* @author ${geneGlobalConfig.author}</#if>
<#if geneGlobalConfig?? && geneGlobalConfig.createTime>* @date ${.now}</#if>
*/
@RestController
@RequestMapping("/${geneTable.tableSubName}")
public class ${geneTable.tableName}Controller {

    @Autowired
    private ${geneTable.tableName}Service ${geneTable.tableSubName}Service;

    /**
     * 通过id获取元素
     */
    @GetMapping("/{${geneTable.tableSubName}Id}")
    public Result<${geneTable.tableName}> get(@PathVariable String ${geneTable.tableSubName}Id) {
        ${geneTable.tableName} ${geneTable.tableSubName} = ${geneTable.tableSubName}Service.getById(${geneTable.tableSubName}Id);
        return Result.success(${geneTable.tableSubName});
    }

    /**
     * 获取列表全部
     */
    @GetMapping
    public Result<${list}<${geneTable.tableName}>> list() {
        List<${geneTable.tableName}> list = ${geneTable.tableSubName}Service.list();
        return Result.success(list);
    }

    /**
     * 添加元素(无需主键)
     */
    @PostMapping
    public Result<${Boolean}> save(@RequestBody ${geneTable.tableName} ${geneTable.tableSubName}) {

        boolean result = ${geneTable.tableSubName}Service.saveOrUpdate(${geneTable.tableSubName});
        return Result.success(result);
    }

    /**
     * 修改元素(需要主键)
     */
    @PutMapping("/")
    public Result<${Boolean}> update(@RequestBody ${geneTable.tableName} ${geneTable.tableSubName}) {

        boolean result = ${geneTable.tableSubName}Service.saveOrUpdate(${geneTable.tableSubName});
        return Result.success(result);
    }

    /**
     * 通过id删除元素
     */
    @DeleteMapping("/{${geneTable.tableSubName}Id}")
    public Result<${Boolean}> delete(@PathVariable String ${geneTable.tableSubName}Id) {
        boolean result = ${geneTable.tableSubName}Service.removeById(${geneTable.tableSubName}Id);
        return Result.success(result);
    }

    /**
    * 分页查询
    *
    * @param pageNum  当前页
    * @param pageSize 单页大小
    */
    @GetMapping("/{pageNum}/{pageSize}")
    public PageResult<${list}<${geneTable.tableName}>> page(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {

        QueryWrapper<${geneTable.tableName}> wrapper = new QueryWrapper<>();
        wrapper.orderBy(true, false, "sort");

        IPage<${geneTable.tableName}> iPage = ${geneTable.tableSubName}Service.page(new Page<>(pageNum, pageSize), wrapper);

        PageInfo pageInfo = new PageInfo(iPage.getCurrent(), iPage.getSize(), iPage.getTotal(), iPage.getPages());

        return new PageResult<>(Result.success(iPage.getRecords()), pageInfo);
    }

}
